#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import requests

import logging
import traceback


def get_lat_lon(address):
    dic = {}
    try:
        url = 'http://demoapi.easytimestudio.com/tencentmap/ws/geocoder?address={}'.format(address)
        r = requests.get(url)
        js = r.json()
        if 'status' in js and js['status'] == 0:
            result = js['result']
            dic['latitude'] = result['location']['lat']
            dic['longitude'] = result['location']['lng']
            dic['txProvince'] = result['address_components']['province']
            dic['txCity'] = result['address_components']['city']
            dic['txDistrict'] = result['address_components']['district']
            dic['adcode'] = result['ad_info']['adcode']
    except Exception as e:
        logging.error(e)
        logging.error(traceback.format_exc())
    return dic
